import 'package:enjoy_plus/pages/house/building_list.dart';
import 'package:enjoy_plus/pages/house/house_detail.dart';
import 'package:enjoy_plus/pages/house/house_form.dart';
import 'package:enjoy_plus/pages/house/house_list.dart';
import 'package:enjoy_plus/pages/house/location_list.dart';
import 'package:enjoy_plus/pages/house/room_list.dart';
import 'package:enjoy_plus/pages/login/index.dart';
import 'package:enjoy_plus/pages/notice/index.dart';
import 'package:enjoy_plus/pages/profile/index.dart';
import 'package:enjoy_plus/pages/tab_bar_page.dart';
import 'package:enjoy_plus/stores/counter.dart';
import 'package:enjoy_plus/utils/token.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(
    MultiProvider(providers: [
      ChangeNotifierProvider(create: (_)=> CounterModel())
    ],
    child:MaterialApp(
    // 路由
    routes: {
      '/': (context) => const TabBarPage(),
      '/detail': (context) => const NoticeDetail(),
      '/login':(context)=> const LoginPage(),
    },
    // 设置初始路由
    initialRoute: '/',
    // 未找到路由时
    onGenerateRoute: (settings) {
      final tokenManager = TokenManager();
      final token = tokenManager.getToken() ?? '';
      //如果没有token，而且去的页面不是登录页跳转登录页
      if (token.isEmpty && settings.name != '/login') {
        return MaterialPageRoute(builder: (context) => const LoginPage());
      }
      //去编辑个人信息页面
      if (settings.name == '/profile') {
        return MaterialPageRoute(builder: (context) => 
        ProfilePage(userInfo: settings.arguments as Map));
      }
      //去我的房屋页面
      if (settings.name == '/house_list') {
        return MaterialPageRoute(
            builder: (context) =>
                const HouseList());
      }
      //去我的位置页面
      if (settings.name == '/location_list') {
        return MaterialPageRoute(
            builder: (context) =>
                const LocationList());
      }
      // 分步表单2: 选择楼栋
if (settings.name == '/building_list') {
  Map params = settings.arguments as Map;
  return MaterialPageRoute(
    builder: (context) => BuildingList(
      point: params['point'], // 传递小区信息
    ),
  );
}
//房间列表
if (settings.name == '/room_list') {
  Map params = settings.arguments as Map;
  return MaterialPageRoute(
    builder: (context) => RoomList(
      point: params['point'],
      building: params['building'],
      // 传递小区信息
    ),
  );
}
//添加房屋
if (settings.name == '/house_form') {
  //Map params = settings.arguments as Map;
  return MaterialPageRoute(
    builder: (context) => HouseForm(
      params: settings.arguments as Map,
      //point: params['point'],
      //building: params['building'],
      // 传递小区信息
    ),
  );
}
//房屋详情
if (settings.name == '/house_detail') {
  return MaterialPageRoute(
      builder: (context) => HouseDetail(id: (settings.arguments as Map)['id']));
}
      //404
      //return null;
      //没有匹配到直接去主页面
      return MaterialPageRoute(
          builder: (context) =>
              const TabBarPage());
    },
    
  ))
  );
}